package com.jmgd.cjlx.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jmgd.cjlx.constants.MainConstant;
import com.jmgd.cjlx.dao.JmgdMapper;
import com.jmgd.cjlx.dto.EmpsDto;
import com.jmgd.cjlx.service.QwService;
import com.jmgd.cjlx.util.DateUtil;
import com.jmgd.cjlx.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class QwServiceImpl implements QwService {
    @Autowired
    JmgdMapper jmgdMapper;

    @Override
    public String getToken() {
        Map map = HttpClientUtil.doGet(MainConstant.get_token_url);
        String data = (String)map.get("data");
        //  String token = (String)map.get("token");
        JSONObject jsonObject = JSONObject.parseObject(data);
        String token = jsonObject.getString("access_token");
        return token;
    }

    @Override
    public String getDeptmentList(String token) {
        if(token==null){
            token = getToken();
        }
        String getDeptListUrl = MainConstant.get_department_list;
        getDeptListUrl = getDeptListUrl.replaceAll("ACCESS_TOKEN",token);
        Map map = HttpClientUtil.doGet(getDeptListUrl);
        String data = (String)map.get("data");
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray jsonArray = jsonObject.getJSONArray("department");
        for(int i=0;i<jsonArray.size();i++){
            String name = ((JSONObject)jsonArray.get(i)).getString("name");
            String id = ((JSONObject)jsonArray.get(i)).getString("id");
           // System.out.println("部门:"+name);
           // System.out.println("部门:"+id);
            if(name.startsWith("342")){
             //   System.out.println();
            }
            this.getEmpList(token,id);

           // System.out.println(id);
        }

        //jsonArray.get()
        //for()
        return null;
    }

    @Override
    public String getEmpList(String token,String deptId) {
        if(token==null){
            token = getToken();
        }
        String getEmpListUrl = MainConstant.get_user_list;
        getEmpListUrl = getEmpListUrl.replaceAll("ACCESS_TOKEN",token);
        getEmpListUrl = getEmpListUrl.replaceAll("DEPARTMENT_ID",deptId);

        Map map = HttpClientUtil.doGet(getEmpListUrl);
        String data = (String)map.get("data");
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray jsonArray = jsonObject.getJSONArray("userlist");
        String[] userIdList = new String[jsonArray.size()];
        for(int i=0;i<jsonArray.size();i++){
            String name = ((JSONObject)jsonArray.get(i)).getString("name");
            String userid = ((JSONObject)jsonArray.get(i)).getString("userid");
            System.out.println(userid+"主部门："+((JSONObject)jsonArray.get(i)).getString("main_department"));
            userIdList[i] = userid;
        }
        //getCustomerList(token,userIdList);
        //jsonArray.get()
        //for()
        return null;
    }



    @Override
    public String getCustomerList(String token,String[] userIdList) {
        if(token==null){
            token = getToken();
        }
        String getCustomerListUrl = MainConstant.get_customer_list;
        getCustomerListUrl = getCustomerListUrl.replaceAll("ACCESS_TOKEN",token);
        //getCustomerListUrl = getCustomerListUrl.replaceAll("DEPARTMENT_ID",deptId);
        EmpsDto empsDto = new EmpsDto();
        empsDto.setCursor("");
        empsDto.setLimit(100);
        empsDto.setUserid_list(userIdList);
        JSONObject jsonObjectParam = (JSONObject) JSONObject.toJSON(empsDto);

        Map map = HttpClientUtil.doPost(getCustomerListUrl,null,jsonObjectParam);
        String data = (String)map.get("data");
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray jsonArray = jsonObject.getJSONArray("external_contact_list");
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject1 = ((JSONObject)jsonArray.get(i)).getJSONObject("external_contact");
            System.out.println(jsonObject1.get("external_userid"));

            JSONObject jsonObject2 = ((JSONObject)jsonArray.get(i)).getJSONObject("follow_info");
            System.out.println(DateUtil.timeStamp2Date(jsonObject2.get("createtime").toString(),null));

            System.out.println(jsonObject2.get("userid"));
            System.out.println(jsonObject1.get("name"));

            String userid = jsonObject2.get("userid").toString();
            String externalUserid = jsonObject1.get("external_userid").toString();
            String createtime = DateUtil.timeStamp2Date(jsonObject2.get("createtime").toString(),null);


           // jmgdMapper.insertQywx(userid,externalUserid,createtime);

//            String name = ((JSONObject)jsonArray.get(i)).getString("name");
//            String userid = ((JSONObject)jsonArray.get(i)).getString("userid");
//            System.out.println(name);
//            System.out.println(userid);
        }

        //jsonArray.get()
        //for()
        return null;
    }
}
